package com.sllp.C71S3TsllpShop.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import com.sllp.C71S3TsllpShop.bean.Admin;
import com.sllp.C71S3TsllpShop.bean.User;

/**
 * 拦截器
 * @author 沈俊羽
 *
 */
public class UserInterceptor implements HandlerInterceptor{

	/**
     * 在请求处理之前进行调用（Controller方法调用之前）
     */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if(user!=null){
			    return true;
			}
			response.sendRedirect(request.getContextPath()+"/toLogin.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;//如果设置为false时，被请求时，拦截器执行到此处将不会继续操作
        				//如果设置为true时，请求将会继续执行后面的操作
	}
}